package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.AppointSchool;
import com.xmy.cultivate.entity.Role;
import com.xmy.cultivate.entity.RolePermission;
import com.xmy.cultivate.enums.SourseLeveEnum;
import com.xmy.cultivate.mapper.RoleMapper;
import com.xmy.cultivate.mapper.RolePermissionMapper;
import com.xmy.cultivate.service.IAppointSchoolService;
import com.xmy.cultivate.service.IRolePermissionService;
import com.xmy.cultivate.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-08-07
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    RoleMapper roleMapper;

    @Autowired
    RolePermissionMapper rolePermissionMapper;

    @Autowired
    IRolePermissionService iRolePermissionService;

    @Autowired
    IAppointSchoolService iAppointSchoolService;

    @Transactional
    public boolean saveTo(Role role){
        if(role.getId() == null){
            roleMapper.insert(role);
        }else{
            roleMapper.updateById(role);
        }
        iAppointSchoolService.remove(new QueryWrapper<AppointSchool>().eq("for_id",role.getId()).eq("type",5));
        if(role.getGovernMode().getKey() == 2){
            List<AppointSchool> appointSchoolList = new ArrayList<>();
            for (Long id:role.getSchoolIdList()){
                AppointSchool appointSchool = new AppointSchool();
                appointSchool.setForId(role.getId());
                appointSchool.setSchoolId(id);
                appointSchool.setType(5);
                appointSchoolList.add(appointSchool);
            }
            if(appointSchoolList.size()>0){
                iAppointSchoolService.saveBatch(appointSchoolList,appointSchoolList.size());
            }
        }


        List<RolePermission> rolePermissionList = new ArrayList<>();
        iRolePermissionService.remove(new QueryWrapper<RolePermission>().eq("role_id",role.getId()));
        for (Long id:role.getPermissionIdList()){
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(role.getId());
            rolePermission.setPermissionId(id);
            rolePermissionList.add(rolePermission);
        }
        if(rolePermissionList.size()>0){
            iRolePermissionService.saveBatch(rolePermissionList,rolePermissionList.size());
        }
        return true;
    }

    public List<Role> allRole(Wrapper wrapper){
        List<Role> roleList = roleMapper.allRole(wrapper);
        for(Role role: roleList){
            //System.out.print("role.getId:"+role.getId());
            List<Long> schoolIdList = iAppointSchoolService.getSchoolIdListForForId(role.getId(),5);
            role.setSchoolIdList(schoolIdList);

            List<Long> roleIdList = rolePermissionMapper.getIdListForRoleId(role.getId());
            role.setPermissionIdList(roleIdList);
        }
        return roleList;
    }

    @Override
    public  SourseLeveEnum getSourseLeve(Long adminId){
        return roleMapper.getSourseLeve(adminId);
    }

}
